package com.niit.notification.mapper;

import com.niit.notification.entity.Notification;
import org.apache.ibatis.annotations.*;
import java.util.Map;
import java.util.List;

/**
 * 通知Mapper接口
 * 注意：我们不再使用单独的通知表，而是直接从现有表中获取通知数据
 */
@Mapper
public interface NotificationMapper {
    
    /**
     * 获取点赞通知数量
     */
    @Select("SELECT COUNT(*) FROM user_activity_like l JOIN tb_activity a ON l.activity_id = a.id WHERE a.user_id = #{userId}")
    int getLikeNotificationCount(@Param("userId") Long userId);
    
    /**
     * 获取评论通知数量
     */
    @Select("SELECT COUNT(*) FROM tb_comment c JOIN tb_activity a ON c.activity_id = a.id WHERE a.user_id = #{userId}")
    int getCommentNotificationCount(@Param("userId") Long userId);
    
    /**
     * 获取关注通知数量
     */
    @Select("SELECT COUNT(*) FROM tb_follow f WHERE f.following_id = #{userId}")
    int getFollowNotificationCount(@Param("userId") Long userId);

    /**
     * 获取点赞通知
     */
    @Select("SELECT l.id, l.user_id as sender_id, u.nickname as sender_name, u.avatar as sender_avatar, " +
            "a.id as activity_id, a.content as activity_content, l.updated_at as create_time " +
            "FROM user_activity_like l " +
            "JOIN tb_user u ON l.user_id = u.id " +
            "JOIN tb_activity a ON l.activity_id = a.id " +
            "WHERE a.user_id = #{userId} " +
            "ORDER BY l.updated_at DESC " +
            "LIMIT #{offset}, #{size}")
    List<Map<String, Object>> getLikeNotifications(@Param("userId") Long userId, @Param("offset") int offset, @Param("size") int size);
    
    /**
     * 获取点赞通知并转换为Notification实体
     */
    @Select("SELECT l.id, a.user_id as receiver_id, l.user_id as sender_id, " +
            "1 as type, a.id as resource_id, 1 as resource_type, " +
            "l.updated_at as create_time, l.updated_at as update_time " +
            "FROM user_activity_like l " +
            "JOIN tb_activity a ON l.activity_id = a.id " +
            "WHERE a.user_id = #{userId} " +
            "ORDER BY l.updated_at DESC " +
            "LIMIT #{offset}, #{size}")
    List<Notification> getLikeNotificationsEntity(@Param("userId") Long userId, @Param("offset") int offset, @Param("size") int size);

    /**
     * 获取评论通知
     */
    @Select("SELECT c.id, c.user_id as comment_user_id, c.activity_id, c.content as comment_content, " +
            "c.update_date as comment_time, a.content as activity_content, u.nickname as comment_user_name, " +
            "u.avatar as comment_user_avatar " +
            "FROM tb_comment c " +
            "JOIN tb_activity a ON c.activity_id = a.id " +
            "JOIN tb_user u ON c.user_id = u.id " +
            "WHERE a.user_id = #{userId} " +
            "ORDER BY c.update_date DESC LIMIT #{offset}, #{limit}")
    List<Map<String, Object>> getCommentNotifications(@Param("userId") Long userId, @Param("offset") int offset, @Param("limit") int limit);

    /**
     * 获取关注通知
     */
    @Select("SELECT f.follower_id as follower_id, f.following_id, f.create_date as follow_time, " +
            "u.nickname as follower_name, u.avatar as follower_avatar " +
            "FROM tb_follow f " +
            "JOIN tb_user u ON f.follower_id = u.id " +
            "WHERE f.following_id = #{userId} " +
            "ORDER BY f.create_date DESC LIMIT #{offset}, #{limit}")
    List<Map<String, Object>> getFollowNotifications(@Param("userId") Long userId, @Param("offset") int offset, @Param("limit") int limit);
}